package com.nulp.psycho.db.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.nulp.psycho.db.ConnectionManager;
import com.nulp.psycho.db.dao.BaseDao;
import com.nulp.psycho.db.mapping.Converter;
import com.nulp.psycho.db.model.Question;

public class QuestionDao implements BaseDao{

	private final String ADD_QUESTION = "INSERT INTO question (test_id, text) VALUES (?,?)";
	private final String GET_QUESTION_BY_ID = "SELECT * FROM question WHERE id = ?";
	private final String REMOVE_QUESTION = "DELETE FROM question WHERE id = ?";
	private final String UPDATE_QUESTION = "UPDATE question SET test_id=?, text=? WHERE id = ?";
	
	private final String GET_ALL_QUESTIONS_BY_TEST_ID = "SELECT * FROM question WHERE test_id = ?";
	
	@Override
	public <T> void create(T element) {
		Question temp = (Question) element;
		Connection connection = ConnectionManager.getInstance().getConnection();
		try (PreparedStatement preparedStatement = connection
				.prepareStatement(ADD_QUESTION)) {
			preparedStatement.setInt(1, temp.getTestId());
			preparedStatement.setString(2, temp.getText());
			preparedStatement.executeUpdate();
		} catch (SQLException e) {
		} finally {
			ConnectionManager.getInstance().release(connection);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public <T> T read(Integer id) {
		Connection connection = ConnectionManager.getInstance().getConnection();
		Question question = null;
		try (PreparedStatement statement = connection
				.prepareStatement(GET_QUESTION_BY_ID)) {
			statement.setInt(1, id);
			ResultSet rs = statement.executeQuery();
			if (rs.next())
				question = Converter.<Question> fromResaultSetToObject(rs,
						Question.class);
		} catch (SQLException e) {
		} finally {
			ConnectionManager.getInstance().release(connection);
		}
		return (T) question;		
	}

	@Override
	public void delete(Integer id) {
		Connection connection = ConnectionManager.getInstance().getConnection();
		try (PreparedStatement preparedStatement = connection
				.prepareStatement(REMOVE_QUESTION)) {
			preparedStatement.setInt(1, id);
			preparedStatement.executeUpdate();
		} catch (SQLException e) {
		} finally {
			ConnectionManager.getInstance().release(connection);
		}
	}

	@Override
	public <T> void update(T element) {
		Question temp = (Question) element;
		Connection connection = ConnectionManager.getInstance().getConnection();
		try (PreparedStatement preparedStatement = connection
				.prepareStatement(UPDATE_QUESTION)) {
			preparedStatement.setInt(3, temp.getId());
			preparedStatement.setInt(1, temp.getTestId());
			preparedStatement.setString(2, temp.getText());
			preparedStatement.executeUpdate();
		} catch (SQLException e) {
		} finally {
			ConnectionManager.getInstance().release(connection);
		}
	}
	
	@SuppressWarnings("unchecked")
	public <T> T getAllQuestionsByTestId(Integer testId) {
		Connection connection = ConnectionManager.getInstance().getConnection();
		List<Question> questions = new ArrayList<Question>();
		try (PreparedStatement statement = connection.prepareStatement(GET_ALL_QUESTIONS_BY_TEST_ID)) {
			statement.setInt(1, testId);
			ResultSet rs = statement.executeQuery();
			while (rs.next()) {
				questions.add(Converter.<Question> fromResaultSetToObject(rs, Question.class));
			}
		} catch (SQLException e) {
		} finally {
			ConnectionManager.getInstance().release(connection);
		}
		return (T) questions;
	}
	
	
}
